package jedis;

import org.apache.commons.lang.StringUtils;
import redis.clients.jedis.Jedis;
import java.util.HashMap;
import java.util.Map;
import java.util.*;

/**
 * Created by zhangjinxia on 2017/10/12.
 */
public class Redis {

    public static void lpush(String key, String value){
        Jedis jedis = RedisUtil.getJedis();
        jedis.lpush(key, StringUtils.isEmpty(value) ? "" : value);
        RedisUtil.returnResource(jedis);
    }

    public static void hset(String key, String field, String value){
        Jedis jedis = RedisUtil.getJedis();
        jedis.hset(key, field, StringUtils.isEmpty(value) ? "" :value);
        RedisUtil.returnResource(jedis);
    }

    public static void hsetAll(String key, Map<String,String> map){
        Jedis jedis = RedisUtil.getJedis();
        Set<String> set = map.keySet();
        Iterator<String> it = set.iterator();
        while (it.hasNext()){
            String mapKey = it.next();
            jedis.hset(key, mapKey, map.get(mapKey));
        }
        RedisUtil.returnResource(jedis);
    }

    public static String hget(String key, String field){
        Jedis jedis = RedisUtil.getJedis();
        String val = jedis.hget(key, field);
        RedisUtil.returnResource(jedis);
        return val;
    }

    public static Long incr(String key){
        Jedis jedis = RedisUtil.getJedis();
        Long val = jedis.incr(key);
        RedisUtil.returnResource(jedis);
        return val;
    }

    public static Long decr(String key){
        Jedis jedis = RedisUtil.getJedis();
        Long val = jedis.decr(key);
        RedisUtil.returnResource(jedis);
        return val;
    }

    public static Map<String,String> hgetAll(String key){
        Map<String,String> result = new HashMap<>();
        Jedis jedis = RedisUtil.getJedis();
        result = jedis.hgetAll(key);
        RedisUtil.returnResource(jedis);
        return result;
    }

    public static void del(String key){
        Jedis jedis = RedisUtil.getJedis();
        jedis.del(key);
        RedisUtil.returnResource(jedis);
    }

    public static void set(String key, String val){
        Jedis jedis = RedisUtil.getJedis();
        jedis.set(key,val);
        RedisUtil.returnResource(jedis);
    }

    public static String get(String key){
        String result = "";
        Jedis jedis = RedisUtil.getJedis();
        result = jedis.get(key);
        RedisUtil.returnResource(jedis);
        return result;
    }

    public static void lrem(String key, long count, String value){
        Jedis jedis = RedisUtil.getJedis();
        jedis.lrem(key, count, value);
        RedisUtil.returnResource(jedis);
    }

    public static List<String> lAll(String key){
        List<String> result = new ArrayList<>();
        Jedis jedis = RedisUtil.getJedis();
        Long len = jedis.llen(key);
        if (len==0){
            return result;
        }
        result = jedis.lrange(key, 0, len-1);
        RedisUtil.returnResource(jedis);
        return result;
    }

}
